<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 新建客户服务分组 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row">
              <label class="control-label col-3 text-end"> 分组代码 </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="code" />
                <span class="text-muted small"
                  >建议用字母或数字标识，长度不超过5</span
                >
              </div>
            </div>
            <div class="row">
              <label class="control-label col-3 text-end"> 分组名称 </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="name" />
                <span class="text-muted small">最大50个字符</span>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "customerServiceGroupModal",
    },
    departments: {
      type: Array,
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      id: 0,
      code: "",
      name: "",

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        id: this.id,
        code: this.code,
        name: this.name,
      });
    },
    modal: function (v) {
      console.log(v);
      this.id = v.id;
      this.name = v.name;
      this.code = v.code;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
